package praca.impl;

import java.util.StringTokenizer;

import praca.framework.MusicGL;
import praca.framework.SoundManagerGL;
import praca.framework.TextureGL;
import praca.framework.Vector;
import praca.framework.Vertices;
import praca.interfejsy.Game;
import praca.interfejsy.Music;
import praca.interfejsy.Screen;
import praca.interfejsy.SoundManager;
import praca.interfejsy.Texture;
import praca.interfejsy.TouchEvent;
import android.util.Log;

public class HelpScreen extends Screen{

	final static int NR_BRICKS = 100;
	final static int ATLAS_WIDTH = 384;
	final static int ATLAS_HEIGHT = 512;

	final static int PENGUIN_WIDTH = 90;
	final static int PENGUIN_HEIGHT = 2*PENGUIN_WIDTH;
	
	
	final static int ATLAS_WIDTH2 = 512;
	final static int ATLAS_HEIGHT2 = 512;
	final static int FONT_WIDTH = 45;
	final static int FONT_HEIGHT = FONT_WIDTH;
	
	Texture alfabet;
	Texture background;
	Texture title;
	Texture menuBrick;
	Texture penguin;
	Texture back;
	Music music;
	SoundManager sounds;
	int clickSound;
	
	Vertices backgroundVert = new Vertices(800, 480, true);
	Vertices titleVert = new Vertices(500, 180, true);
	Vertices backVert = new Vertices(128, 128, true);
	Vertices brickVert = new Vertices(50, 50, true);
	Vertices penguinVert = new Vertices(0,0,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices penguinVert2 = new Vertices(128,0,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices penguinVert3 = new Vertices(256,0,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	
	Vertices revPenguinVert = new Vertices(0,256,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices revPenguinVert2 = new Vertices(128,256,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	Vertices revPenguinVert3 = new Vertices(256,256,128,256, PENGUIN_WIDTH,PENGUIN_HEIGHT, ATLAS_WIDTH, ATLAS_HEIGHT);
	
	Vertices fontVer = new Vertices(0,448,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
	
	Vector penguinPosition = new Vector(64,PENGUIN_WIDTH);
	Vector penguinVelocity = new Vector(40, 0);
	
	
	
	Vector gravity = new Vector(0, 0);
	float time;
	boolean isGoingRight = true;
	
	MenuBrick[] bricks;
	private TouchEvent ev;
	
	
	public HelpScreen(Game game) {
		super(game);

		bricks = new MenuBrick[NR_BRICKS];

		
		
		for(int i = 0 ; i < NR_BRICKS; i++){
			bricks[i] = new MenuBrick();
		}
		
		music = new MusicGL(game, "menu.mp3");
		sounds = new SoundManagerGL(game);
		clickSound = sounds.loadSound("click.ogg");
		

	}

	@Override
	public void update(float deltaTime) {

		if(!in.getTouchEvents().isEmpty()){
			ev = in.getTouchEvents().remove(0);
			
			if(ev.getType() == TouchEvent.TOUCH_UP){
				if(checkIfBack(ev.getX(), ev.getY())){
					
					sounds.play(clickSound);
					game.setCurrentScreen(new MenuScreen(game));
				}
				if(ev.getX() == -100 && ev.getY()==-100)
					game.setCurrentScreen(new MenuScreen(game));
				}//touchup
				
			
		}//isEmpty
		
		
		g.clearTransformations();
		g.moveTexture(400, 240);
		g.drawTexture(background, backgroundVert);
		

		g.setTransparency(0.7f);
		
		for(int i = 0; i<NR_BRICKS; i++){
			g.clearTransformations();
			g.moveTexture(bricks[i].position.x, bricks[i].position.y);
			bricks[i].angle += bricks[i].rotationSpeed*deltaTime;
			g.rotateTexture(bricks[i].angle);
			g.drawTexture(menuBrick, brickVert);
		}
		
		for(int i = 0; i<NR_BRICKS; i++){
			bricks[i].velocity.add(gravity.x *deltaTime, gravity.y*deltaTime);
			bricks[i].position.add(bricks[i].velocity.x * deltaTime, bricks[i].velocity.y * deltaTime);
			if(bricks[i].position.y < -50)
				bricks[i].position.y = 550;
			if(bricks[i].angle>360)
				bricks[i].angle = 0;
		}
		
		time = time + deltaTime;
		g.setTransparency(1);
		g.clearTransformations();
		g.moveTexture(penguinPosition.x, penguinPosition.y);
		
		if(isGoingRight){
			if(time<=0.2){
				g.drawTexture(penguin, penguinVert);
			}else if(time<=0.4){
				g.drawTexture(penguin, penguinVert2);
			}else if(time<=0.6){
				g.drawTexture(penguin, penguinVert3);
				
			}else if(time<=0.8){
				g.drawTexture(penguin, penguinVert2);
				
			}else{
				g.drawTexture(penguin, penguinVert2);
				time=0;
			}
		}else{
			if(time<=0.2){
				g.drawTexture(penguin, revPenguinVert);
			}else if(time<=0.4){
				g.drawTexture(penguin, revPenguinVert2);
			}else if(time<=0.6){
				g.drawTexture(penguin, revPenguinVert3);
				
			}else if(time<=0.8){
				g.drawTexture(penguin, revPenguinVert2);
				
			}else{
				g.drawTexture(penguin, revPenguinVert2);
				time=0;
			}
		}
		
		
		
		
		
		penguinPosition.add(penguinVelocity.x * deltaTime,penguinVelocity.y * deltaTime);
		
		if(penguinPosition.x>870){
			isGoingRight = false;
			penguinVelocity.x=-penguinVelocity.x;
		}
		if(penguinPosition.x<-50){
			isGoingRight = true;
			penguinVelocity.x=-penguinVelocity.x;
		}
		
		g.clearTransformations();
		g.moveTexture(400, 400);
		g.drawTexture(title, titleVert);
		g.clearTransformations();
		g.moveTexture(64, 64);
		g.drawTexture(back, backVert);
			
		

		
	}

	
	
	public void drawText(String text, int xPos, int yPos){
		int space = 30;
		int FONT_SIZE = 64;
		int x = xPos;
		int y =	yPos;
		char[] cArray = text.toUpperCase().toCharArray();
		
		for(int i =0 ; i<cArray.length; i++){
			if(i>12){
				fontVer = new Vertices(192,192,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				x+=10;
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=10;
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=10;
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);

				break;
			}
			
			if(cArray[i]==' '){

				x+=20;
				continue;
			}
			if(cArray[i]=='A'){
				fontVer = new Vertices(0,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='B'){
				fontVer = new Vertices(64,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='C'){
				fontVer = new Vertices(128,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='D'){
				fontVer = new Vertices(192,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='E'){
				fontVer = new Vertices(256,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='F'){
				fontVer = new Vertices(320,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='G'){
				fontVer = new Vertices(384,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='H'){
				fontVer = new Vertices(448,448,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='I'){
				fontVer = new Vertices(0,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='J'){
				fontVer = new Vertices(64,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='K'){
				fontVer = new Vertices(128,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='L'){
				fontVer = new Vertices(192,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='M'){
				fontVer = new Vertices(256,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='N'){
				fontVer = new Vertices(320,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='O'){
				fontVer = new Vertices(384,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='P'){
				fontVer = new Vertices(448,384,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='R'){
				fontVer = new Vertices(0,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='S'){
				fontVer = new Vertices(64,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='T'){
				fontVer = new Vertices(128,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='U'){
				fontVer = new Vertices(192,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='V'){
				fontVer = new Vertices(256,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='W'){
				fontVer = new Vertices(320,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='X'){
				fontVer = new Vertices(384,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='Y'){
				fontVer = new Vertices(448,320,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='Z'){
				fontVer = new Vertices(0,256,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='0'){
				fontVer = new Vertices(64,256,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='1'){
				fontVer = new Vertices(128,256,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='2'){
				fontVer = new Vertices(192,256,FONT_SIZE,FONT_SIZE, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='3'){
				fontVer = new Vertices(256,256,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='4'){
				fontVer = new Vertices(320,256,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='5'){
				fontVer = new Vertices(384,256,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='6'){
				fontVer = new Vertices(448,256,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='7'){
				fontVer = new Vertices(0,192,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='8'){
				fontVer = new Vertices(64,192,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='9'){
				fontVer = new Vertices(128,192,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space;
				continue;
			}
			if(cArray[i]=='.'){
				fontVer = new Vertices(192,192,64,64, FONT_WIDTH,FONT_HEIGHT, ATLAS_WIDTH2, ATLAS_HEIGHT2);
				g.clearTransformations();
				g.moveTexture(x, y);
				g.drawTexture(alfabet, fontVer);
				x+=space/3;
				continue;
			}
		}
		
	}
	
	@Override
	public void pause() {
		
	}

	@Override
	public void resume() {
		background = new TextureGL(game, "menu_tlo2.png");
		title = new TextureGL(game, "title2.png");
		menuBrick = new TextureGL(game, "snowflake.png");
		penguin = new TextureGL(game, "atlas.png");
		back = new TextureGL(game, "back.png");
		alfabet = new TextureGL(game,"atlas_liter.png");
		g.setTextureTransparentBackground();
	}

	@Override
	public void dispose() {
		
		
	}
	
	private boolean checkIfBack(float x, float y){
		if(x < 128 && x>0 && y>0 && y < 128)
			return true;
					return false;
	}

}
